package ca.eloas.gpsclock.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * @author JP
 */
public class AlarmDatabase extends SQLiteOpenHelper {

    private static int DATABASE_VERSION = 3;
    private static String DATABASE_NAME = "AlarmDatabase";

    private static final String DICTIONARY_TABLE_CREATE =
            "CREATE TABLE TIME_ALARMS (" +
                    "_id  INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    "TIME TEXT, " +
                    "ACTIVE INTEGER);";

    public AlarmDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {

        Log.d("DB", "creating database");
        db.execSQL(DICTIONARY_TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int old, int newv) {

//        db.execSQL("alter table time_alarms add column  ACTIVE INTEGER ;");
    }

    public long saveAlarm(TimeAlarmInfo a) {

        SQLiteDatabase db = getWritableDatabase();
        if ( a.isSaved() ) {
            
            try {
                db.beginTransaction();
                ContentValues cv = alarmToValues(a);
                db.update("TIME_ALARMS", cv, "_id = '" + a.getId() + "'", null);
                db.setTransactionSuccessful();
                return a.getId();
            } finally {
                
                db.endTransaction();
            }
        } else {

            try {
                db.beginTransaction();
                ContentValues cv = alarmToValues(a);
                long ret = db.insert("TIME_ALARMS", null, cv);
                db.setTransactionSuccessful();
                return ret;
            } finally {

                db.endTransaction();
            }
        }
    }

    public List<TimeAlarmInfo> getAllAlarms() {

        List<TimeAlarmInfo> list = new ArrayList<TimeAlarmInfo>();
        SQLiteDatabase db = getReadableDatabase();
        try {
            db.beginTransaction();
            Cursor c = db.query("TIME_ALARMS", new String[]{"_id", "TIME", "ACTIVE"}, null, null, null, null, "_id");
            try {
                c.moveToFirst();
                while ( ! c.isAfterLast() ) {
    
                    long id = c.getInt(0);
                    String[] times = c.getString(1).split(":");
                    TimeAlarmInfo ta = new TimeAlarmInfo(id, Integer.parseInt(times[0],10), Integer.parseInt(times[1], 10));
                    ta.setActive(c.getInt(2) == 1);
                    list.add(ta);
                    c.moveToNext();
    
                }
            } finally {
                
                c.close();
            }
        } finally {

            db.endTransaction();
        }

        return list;
    }

    public void deleteAlarm(TimeAlarmInfo a) {

        SQLiteDatabase db = getWritableDatabase();
        try {
            db.beginTransaction();
            db.delete("TIME_ALARMS", "_id = '" + a.getId() + "'", null);
            db.setTransactionSuccessful();
        } finally {

            db.endTransaction();
        }
    }

    public void deleteAllAlarms() {

        SQLiteDatabase db = getWritableDatabase();
        db.beginTransaction();
        try {
            db.delete("TIME_ALARMS", null, null);
            db.setTransactionSuccessful();
        } finally {

            db.endTransaction();
        }
    }

    
    private ContentValues alarmToValues(TimeAlarmInfo a) {
        
        ContentValues cv = new ContentValues();
        cv.put("TIME", a.asString());
        cv.put("ACTIVE", (a.getActive()?1:0));
        if ( a.isSaved()) {
            
     //       cv.put("_id", a.getId());
        }

        return cv;
    }
}


